package com.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.iot.commons.ResponseResult;
import com.iot.enums.StatusEnum;
import com.iot.mapper.ElectronicFenceRecordMapper;
import com.iot.model.entity.ElectronicFenceRecordEntity;
import com.iot.model.query.ElectronicFenceRecordQuery;
import com.iot.model.dto.ElectronicFenceRecordDto;
import com.iot.model.vo.ElectronicFenceRecordVo;
import com.iot.service.ElectronicFenceRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.iot.utils.ResponseResultUtil;

import java.util.List;

/**
 * ElectronicFenceRecordServiceImpl
 *
 * @author: dh
 * @date: 2024-06-15 15:49:04
 */
@Service
@RequiredArgsConstructor
public class ElectronicFenceRecordServiceImpl implements ElectronicFenceRecordService {

    private final ElectronicFenceRecordMapper electronicFenceRecordMapper;

    @Override
    public ResponseResult getList(ElectronicFenceRecordQuery query) {
        Page<ElectronicFenceRecordEntity> page = new Page<>(query.getPageIndex(), query.getPageSize());
        LambdaQueryWrapper<ElectronicFenceRecordEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ElectronicFenceRecordEntity::getFollowId, query.getId());
        if (null != query.getStartTime()) {
            queryWrapper.ge(ElectronicFenceRecordEntity::getTime, query.getStartTime());
        }
        if (null != query.getEndTime()) {
            queryWrapper.le(ElectronicFenceRecordEntity::getTime, query.getEndTime());
        }
        queryWrapper.eq(ElectronicFenceRecordEntity::getState, StatusEnum.VALID.getKey());
        queryWrapper.orderByDesc(ElectronicFenceRecordEntity::getTime);
        IPage<ElectronicFenceRecordEntity> electronicFenceRecordPage = electronicFenceRecordMapper.selectPage(page, queryWrapper);
        List<ElectronicFenceRecordVo> electronicFenceRecordVoList = Lists.newArrayList();
        for (ElectronicFenceRecordEntity electronicFenceRecordEntity : electronicFenceRecordPage.getRecords()) {
            ElectronicFenceRecordVo vo = new ElectronicFenceRecordVo();
            vo.setId(electronicFenceRecordEntity.getId());
            vo.setNickName(electronicFenceRecordEntity.getNickName());
            vo.setFenceId(electronicFenceRecordEntity.getFenceId());
            vo.setFenceName(electronicFenceRecordEntity.getFenceName());
            vo.setTime(electronicFenceRecordEntity.getTime());
            vo.setLon(electronicFenceRecordEntity.getLon());
            vo.setLat(electronicFenceRecordEntity.getLat());
            vo.setFenceLon(electronicFenceRecordEntity.getFenceLon());
            vo.setFenceLat(electronicFenceRecordEntity.getFenceLat());
            vo.setFenceRadius(electronicFenceRecordEntity.getFenceRadius());
            electronicFenceRecordVoList.add(vo);
        }

        return ResponseResultUtil.success(electronicFenceRecordPage.getTotal(), electronicFenceRecordVoList);
    }

    @Override
    public ResponseResult edit(ElectronicFenceRecordDto electronicFenceRecord) {
        ElectronicFenceRecordEntity entity = new ElectronicFenceRecordEntity();
        entity.setId(electronicFenceRecord.getId());
        entity.setAuthId(electronicFenceRecord.getAuthId());
        entity.setFenceName(electronicFenceRecord.getFenceName());
        entity.setState(electronicFenceRecord.getState());
        entity.setTime(electronicFenceRecord.getTime());
        entity.setLon(electronicFenceRecord.getLon());
        entity.setLat(electronicFenceRecord.getLat());
        entity.setFenceLon(electronicFenceRecord.getFenceLon());
        entity.setFenceLat(electronicFenceRecord.getFenceLat());
        entity.setFenceRadius(electronicFenceRecord.getFenceRadius());
        electronicFenceRecordMapper.updateById(entity);
        return ResponseResultUtil.success();
    }

    @Override
    public ResponseResult add(ElectronicFenceRecordDto electronicFenceRecord) {
        ElectronicFenceRecordEntity entity = new ElectronicFenceRecordEntity();
        entity.setId(electronicFenceRecord.getId());
        entity.setFenceId(electronicFenceRecord.getFenceId());
        entity.setFollowId(electronicFenceRecord.getFollowId());
        entity.setNickName(electronicFenceRecord.getNickName());
        entity.setAuthId(electronicFenceRecord.getAuthId());
        entity.setFenceName(electronicFenceRecord.getFenceName());
        entity.setState(StatusEnum.VALID.getKey());
        entity.setTime(electronicFenceRecord.getTime());
        entity.setLon(electronicFenceRecord.getLon());
        entity.setLat(electronicFenceRecord.getLat());
        entity.setFenceLon(electronicFenceRecord.getFenceLon());
        entity.setFenceLat(electronicFenceRecord.getFenceLat());
        entity.setFenceRadius(electronicFenceRecord.getFenceRadius());
        electronicFenceRecordMapper.insert(entity);
        return ResponseResultUtil.success();
    }

    @Override
    public ResponseResult getById(ElectronicFenceRecordQuery electronicFenceRecord) {
        ElectronicFenceRecordEntity electronicFenceRecordEntity = electronicFenceRecordMapper.selectById(electronicFenceRecord.getId());
        ElectronicFenceRecordVo vo = new ElectronicFenceRecordVo();
        vo.setId(electronicFenceRecordEntity.getId());
        vo.setFenceName(electronicFenceRecordEntity.getFenceName());
        vo.setTime(electronicFenceRecordEntity.getTime());
        vo.setLon(electronicFenceRecordEntity.getLon());
        vo.setLat(electronicFenceRecordEntity.getLat());
        vo.setFenceLon(electronicFenceRecordEntity.getFenceLon());
        vo.setFenceLat(electronicFenceRecordEntity.getFenceLat());
        vo.setFenceRadius(electronicFenceRecordEntity.getFenceRadius());
        return ResponseResultUtil.success(vo);
    }

}
